﻿using System;
using System.Collections.Generic;
using System.Text;
using CGLib;
using CGLib.Enums;

namespace CoreGameLib
{
    // TODO: Chop combos?

    /// <summary>
    /// Easy AI player for Tien Len.
    /// </summary>
    public class TLAIEasy : IComputer
    {
        private List<Card>[] cardList = new List<Card>[13];
        internal static bool allowCustomNames = true;

        private string GenerateName()
        {
            string[] names = new string[] { "Lenny", "Carl", "Arnold", "Saffron", "Colin", "Davis", "Folly", "Sherald", "Dennis", "Lion", "Derrick", "Lan" };
            Random ran = new Random();

            return names[ran.Next(0, names.Length)];
        }

        public void Initialised(Player aiPlayer, IGame game)
        {
            // Give AI player's a name.
            if (allowCustomNames)
                aiPlayer.Name = GenerateName();

            for (int i = 0; i < 13; i++)
            {
                int amount = aiPlayer.Cards.FindAll(delegate(Card card) { return Parsers.GetGCV(game, card.Value) == Parsers.GetGCV(game, (Value)i + 1); }).Count;
                cardList[i] = new List<Card>(amount);
            }

            foreach (Card card in aiPlayer.Cards)
                cardList[Parsers.GetGCV(game, card.Value) - 1].Add(card);

            for (int j = 0; j < 13; j++)
            {
                if (cardList[j].Count != 0)
                    Parsers.SortCards(game, cardList[j]);
            }
        }

        public string Name()
        {
            return "Computer (Easy)";
        }

        public List<Card> Process(List<Card> currentCards, IGame game)
        {
            List<Card> cardSend = new List<Card>();

            if (((TienLen)game).FieldType == FieldType.None)
            {
                for (int i = 0; i < 13; i++)
                {
                    if (cardList[i].Count != 0)
                    {
                        ((TienLen)game).FieldType = (FieldType)cardList[i].Count;

                        foreach (Card card in cardList[i])
                            cardSend.Add(card);

                        cardList[i].Clear();
                        break;
                    }
                }
            }
            else
            {
                switch (((TienLen)game).FieldType)
                {
                    case FieldType.Single:

                        for (int i = Parsers.GetGCV(game, currentCards[0].Value) - 1; i < 13; i++)
                        {
                            if (cardList[i].Count == 1 &&
                                (Parsers.GetGCV(game, cardList[i][0].Value) > Parsers.GetGCV(game, currentCards[0].Value) ||
                                (i + 1 == Parsers.GetGCV(game, currentCards[0].Value) && cardList[i][0].Suite > currentCards[0].Suite)))
                            {
                                cardSend.Add(cardList[i][0]);
                                cardList[i].Remove(cardList[i][0]);
                                break;
                            }
                        }

                        break;

                    case FieldType.Double:
                    case FieldType.Triple:
                    case FieldType.FourKind:

                        for (int i = Parsers.GetGCV(game, currentCards[0].Value); i < 13; i++)
                        {
                            if (cardList[i].Count == (int)((TienLen)game).FieldType &&
                                Parsers.GetSuiteSum(cardList[i]) == Parsers.GetSuiteSum(currentCards))
                            {
                                foreach (Card card in cardList[i])
                                    cardSend.Add(card);

                                cardList[i].Clear();
                                break;
                            }
                        }

                        break;
                }
            }

            return cardSend;
        }
    }
}
